/*
 * MIT License
 *
 * Copyright (c) 2024-2025 milkpotatoes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef SRC_JS_UTILS_INCLUDE_JS_UTILS_JS_CHILD_PROCESS_H
#define SRC_JS_UTILS_INCLUDE_JS_UTILS_JS_CHILD_PROCESS_H

#include <cstddef>
#include <cstdint>
#include <napi/native_api.h>
#include <string>
#include <uv.h>
#include <vector>

#include "runner_utils/napi_utils.h"

class JsChildProcess
{
public:
    static napi_value Init(napi_env env, napi_value exports);

private:
    JsChildProcess(napi_env env, napi_value onExit);
    ~JsChildProcess();

    static JsChildProcess *New(napi_env env, napi_value onExit);
    void Delete();
    static void Finalize(napi_env env, void *data, void *hint);
    DECL_NAPI_CALLBACK(InnerSpawn);
    DECL_NAPI_CALLBACK(InnerReadStream);
    DECL_NAPI_CALLBACK(InnerGetFd);
    void OnExit(int status, int signal);
    static void ExitCallback(uv_process_t *process, long status, int signal);
    static void InitStdio(napi_env env, napi_value option, uv_stdio_container_t *stdio, uv_stdio_flags direction,
                          int defaultFd);

    class OptionsParser
    {
    public:
        OptionsParser(napi_env env, napi_value options);
        ~OptionsParser();
        int ParseFlags(napi_env env, napi_value flags);
        inline operator uv_process_options_t *()
        {
            return &options_;
        }
        inline uv_process_options_t *operator->()
        {
            return &options_;
        }

    private:
        std::vector<char *> AddStrings(const std::vector<std::string> &strings);

        std::vector<char *> &ToCharsArray(napi_env env, napi_value array);
        uv_process_options_t options_{};
        std::vector<std::string> strings_{};
        std::vector<std::vector<char *>> vectors_{};
        std::vector<char *> memblocks_{};
    };

    struct ReadStreamData
    {
        napi_env env;
        napi_ref callback;
    };

    const napi_env env_{};
    napi_ref onExit_{};
    uv_process_t subProcess_{};
    uv_stdio_container_t in_{};
    uv_stdio_container_t out_{};
    uv_stdio_container_t err_{};
};

#endif /* SRC_JS_UTILS_INCLUDE_JS_UTILS_JS_CHILD_PROCESS_H */
